%for horizontal grid line optimizing
%hspotcoff1:coordinate value of grid line in horizontal
%flag: flag for setting parameter,0-set,1-no
%flagend: flag for optimizing times
%errorlimit: flag for terminal error
%hspotcoff2: cooridnate value after optimizing
%vdmean:mean distance 
%vdermean:mean distance error
function [hspotcoff2,hdmean, hdermean]=hgridadjust(hspotcoff1,flag,flagend,errorlimit)
[s,hf1]=size(hspotcoff1);
disp(['number of horizontal grid lines��',num2str(hf1)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%distance between two grid lines
for m=1:hf1-1
 hd(m)=hspotcoff1(m+1)-hspotcoff1(m);
end
%figure,plot(1:hf1-1,hd,'mean distance in horizontal');
%mean distance
hdmean=mean(hd);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%distance error
for m=1:hf1-1
 hderror(m)=abs(hd(m)-hdmean);
end
%figure,plot(1:hf1-1,hderror,'distance error profile in horizontal');
%mean distance error
hdermean=mean(hderror);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%mean square error
herror=0;
for m=1:hf1-1
    herror=herror+(hd(m)-hdmean).^2;
end
herror=sqrt(herror/(hf1-1))
disp(['mean distance in horizontal��',num2str(hdmean),'mean distance error in horizontal��',num2str(hdermean)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%estimate the number of wrong grid lines
hernum=0;
for m=2:hf1-2
    if hderror(m)>herror+hdmean/10
        hernum=hernum+1;
        hergridline(hernum)=m;
    end
end
disp(['the wrong grid line numbers in horizontal:',num2str(hernum)]);
if flag==0
    %initialization of flagend and errorlimit
    flagend=hernum;
    errorlimit=2*hdermean;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%find the maximum error
if flagend~=0 && hdermean<errorlimit
if hernum~=0 %&& flagend~=0
       [maxerror,index]=max(hderror);
       disp(['the maximum error is��',num2str(index)]);
       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       %the first grid line
       if index==1
        if hd(index)<hdmean && (hderror(index+1)<herror+hdmean/10)
            disp(['redundant grid line 1:',int2str(index)]);
            hspotcoff2(index)=(hspotcoff1(index)+hspotcoff1(index+1))/2;
           for k=index+1:hf1-1
               hspotcoff2(k)=hspotcoff1(k+1);
           end
        elseif hd(index)<hdmean && (hderror(index+1)>herror+hdmean/10)
           disp(['redundant grid line 2:',int2str(index)]);
           hspotcoff2(index)=hspotcoff1(index);
           hspotcoff2(index+1)=(hspotcoff1(index+1)+hspotcoff1(index+2))/2;
           for k=index+2:hf1-1
               hspotcoff2(k)=hspotcoff1(k+1);
           end
        elseif hd(index)>hdmean && (hderror(index+1)<herror+hdmean/10)
           disp(['missing a grid line 1:',int2str(index)]);
           hspotcoff2(index)=hspotcoff1(index);
           hspotcoff2(index+1)=(hspotcoff1(index)+hspotcoff1(index+1))/2;
           hspotcoff2(index+2:hf1+1)=hspotcoff1(index+1:hf1);
           
        elseif hd(index)>hdmean &&  (hderror(index+1)>herror+hdmean/10)
           disp(['missing a grid line 2:',int2str(index)]);
           hspotcoff2(index)=hspotcoff1(index);
           hspotcoff2(index+1)=hspotcoff1(index)+(hspotcoff1(index+2)-hspotcoff1(index))/3;
           hspotcoff2(index+3:hf1+1)=hspotcoff1(index+2:hf1);
           hspotcoff2(index+2)=hspotcoff1(index)+2*(hspotcoff1(index+2)-hspotcoff1(index))/3;
        else (hd(index)>hdmean && hd(index+1))<hdmean ||(hd(index)<hdmean && hd(index+1)>hdmean)
           disp(['locate at wrong place:',int2str(index)]);
           hspotcoff2=hspotcoff1;
           hspotcoff2(index+1)=(hspotcoff1(index)+hspotcoff1(index+2))/2;
        end
     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     %the last grid line
     elseif index==hf1-1
        if hd(index)<hdmean && (hderror(index-1)<herror+hdmean/10)
            disp(['redundant grid line 1:',int2str(index)]);
            hspotcoff2(1:index-1)=hspotcoff1(1:index-1);
           hspotcoff2(index)=(hspotcoff1(index)+hspotcoff1(index+1))/2;
           
        elseif hd(index)<hdmean && (hderror(index-1)>herror+hdmean/10)
           disp(['redundant grid line 2:',int2str(index)]);
           hspotcoff2(1:index-2)=hspotcoff1(1:index-2);
           hspotcoff2(index-1)=(hspotcoff1(index)+hspotcoff1(index-1))/2;
           hspotcoff2(index)=hspotcoff1(index+1);
           
        elseif hd(index)>hdmean && (hderror(index-1)<herror+hdmean/10)
           disp(['missing a grid line 1:',int2str(index)]);
           hspotcoff2(1:index)=hspotcoff1(1:index);
           hspotcoff2(index+1)=(hspotcoff1(index)+hspotcoff1(index+1))/2;
           hspotcoff2(index+2)=hspotcoff1(index+1);
           
        elseif hd(index)>hdmean &&  (hderror(index-1)>herror+hdmean/10)
           disp(['missing a grid line 2:',int2str(index)]);
           hspotcoff2(1:index-1)=hspotcoff1(1:index-1);
           hspotcoff2(index)=hspotcoff1(index-1)+(hspotcoff1(index+1)-hspotcoff1(index-1))/3;
           hspotcoff2(index+2)=hspotcoff1(index+1);
           hspotcoff2(index+1)=hspotcoff1(index-1)+2*(hspotcoff1(index+1)-hspotcoff1(index-1))/3;
        else (hd(index)>hdmean && hd(index-1))<hdmean ||(hd(index)<hdmean && hd(index-1)>hdmean)
           disp(['locate at wrong place:',int2str(index)]);
           hspotcoff2=hspotcoff1;
           hspotcoff2(index)=(hspotcoff1(index+1)+hspotcoff1(index-1))/2;
        end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    else  
        if (hd(index)<hdmean && (hderror(index-1)<herror+hdmean/10)&& (hderror(index+1)<herror+hdmean/10))||(hd(index)<hdmean && hd(index+1)<hdmean)
            disp(['redundant grid line 1',int2str(index)]);
            hspotcoff2(1:index-1)=hspotcoff1(1:index-1);
           hspotcoff2(index)=(hspotcoff1(index)+hspotcoff1(index+1))/2;
           for k=index+1:hf1-1
               hspotcoff2(k)=hspotcoff1(k+1);
           end
       elseif hd(index)<hdmean && ( (hderror(index+1)>herror+hdmean/10)||( hderror(index-1)>herror+hdmean/10))
           disp(['redundant grid line 2:',int2str(index)]);
           hspotcoff2(1:index)=hspotcoff1(1:index);
           hspotcoff2(index+1)=(hspotcoff1(index+1)+hspotcoff1(index+2))/2;
           for k=index+2:hf1-1
               hspotcoff2(k)=hspotcoff1(k+1);
           end
       elseif hd(index)>hdmean && (hderror(index-1)<herror+hdmean/10)&& (hderror(index+1)<herror+hdmean/10)
           disp(['missing a grid line 1:',num2str(index)]);
           hspotcoff2(1:index)=hspotcoff1(1:index);
           hspotcoff2(index+1)=(hspotcoff1(index)+hspotcoff1(index+1))/2;
           hspotcoff2(index+2:hf1+1)=hspotcoff1(index+1:hf1);
           
       else hd(index)>hdmean && ( (hderror(index-1)>herror+hdmean/10)|| (hderror(index+1)>herror+hdmean/10))
           disp(['missing a grid line 2:',int2str(index)]);
           if  hd(index-1)>hdmean
               hspotcoff2(1:index-1)=hspotcoff1(1:index-1);
               hspotcoff2(index)=hspotcoff1(index-1)+(hspotcoff1(index+1)-hspotcoff1(index-1))/3;
               hspotcoff2(index+1)=hspotcoff1(index-1)+2*(hspotcoff1(index+1)-hspotcoff1(index-1))/3;
               hspotcoff2(index+2:hf1+1)=hspotcoff1(index+1:hf1);
           else
              hspotcoff2(1:index)=hspotcoff1(1:index);
              hspotcoff2(index+1)=hspotcoff1(index)+(hspotcoff1(index+2)-hspotcoff1(index))/3;
              hspotcoff2(index+2)=hspotcoff1(index)+2*(hspotcoff1(index+2)-hspotcoff1(index))/3;
              hspotcoff2(index+3:hf1+1)=hspotcoff1(index+2:hf1);
           end
       end
    
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     flag=flag+1;
     flagend=flagend-1;
     disp(['loop times: ',num2str(flagend)]);
     hgridadjust(hspotcoff2,flag,flagend,errorlimit);
 else
    hspotcoff2=hspotcoff1;
    clear hspotcoff1;
    [s,size1]=size(hspotcoff2);
    disp(['final horizontal grid line number��',num2str(size1)]);
    disp('no error');

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else
    hspotcoff2=hspotcoff1;
end
clear hspotcoff1;
clear flag;
clear flagend;
clear errorlimit;